/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package upc.jfactory.scp.jpa.controlador;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import upc.jfactory.scp.jpa.bean.CompetenciasXOfertaLaboral;
import upc.jfactory.scp.jpa.controlador.exceptions.NonexistentEntityException;
import upc.jfactory.scp.jpa.controlador.exceptions.PreexistingEntityException;

/**
 *
 * @author Diego
 */
public class CompetenciasXOfertaLaboralJpaController {

    public CompetenciasXOfertaLaboralJpaController() {
        emf = Persistence.createEntityManagerFactory("Web-SCPPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
 public CompetenciasXOfertaLaboral createCompetenciasXOfertaLaboral(CompetenciasXOfertaLaboral compoferta) {
              EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(compoferta);
            em.getTransaction().commit();
        } catch (Exception ex) {


        } finally {
            if (em != null) {
                em.close();

            }
               return compoferta;
        }
  }
 public void removeCompetenciasXOfertaLaboral(int id) {
            EntityManager em = getEntityManager();
    CompetenciasXOfertaLaboral emp = findCompetenciasXOfertaLaboral(id);
    if (emp != null) {
      em.remove(emp);
      em.close();
    }
  }
    public void create(CompetenciasXOfertaLaboral competenciasXOfertaLaboral) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(competenciasXOfertaLaboral);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCompetenciasXOfertaLaboral(competenciasXOfertaLaboral.getCompetenciasofertaId()) != null) {
                throw new PreexistingEntityException("CompetenciasXOfertaLaboral " + competenciasXOfertaLaboral + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CompetenciasXOfertaLaboral competenciasXOfertaLaboral) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            competenciasXOfertaLaboral = em.merge(competenciasXOfertaLaboral);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = competenciasXOfertaLaboral.getCompetenciasofertaId();
                if (findCompetenciasXOfertaLaboral(id) == null) {
                    throw new NonexistentEntityException("The competenciasXOfertaLaboral with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CompetenciasXOfertaLaboral competenciasXOfertaLaboral;
            try {
                competenciasXOfertaLaboral = em.getReference(CompetenciasXOfertaLaboral.class, id);
                competenciasXOfertaLaboral.getCompetenciasofertaId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The competenciasXOfertaLaboral with id " + id + " no longer exists.", enfe);
            }
            em.remove(competenciasXOfertaLaboral);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CompetenciasXOfertaLaboral> findCompetenciasXOfertaLaboralEntities() {
        return findCompetenciasXOfertaLaboralEntities(true, -1, -1);
    }

    public List<CompetenciasXOfertaLaboral> findCompetenciasXOfertaLaboralEntities(int maxResults, int firstResult) {
        return findCompetenciasXOfertaLaboralEntities(false, maxResults, firstResult);
    }

    private List<CompetenciasXOfertaLaboral> findCompetenciasXOfertaLaboralEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from CompetenciasXOfertaLaboral as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CompetenciasXOfertaLaboral findCompetenciasXOfertaLaboral(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CompetenciasXOfertaLaboral.class, id);
        } finally {
            em.close();
        }
    }

     public List<CompetenciasXOfertaLaboral> DAMECompetenciasXOfertaLaboral(Integer id) {
        EntityManager em = getEntityManager();
        try {
             Query q= em.createNamedQuery("CompetenciasXOfertaLaboral.findByOfertalaboralId");
             q.setParameter("ofertalaboralId", id);
             return q.getResultList();
        } finally {
            em.close();
        }
    }

    public int getCompetenciasXOfertaLaboralCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from CompetenciasXOfertaLaboral as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
